/* list */

$(function () {
    
    var list = {
        sortType : 0,
        rentType : 0,
        page : 1,
        area : [],
        price : [],
        room : [],
        square : [],
        direction : []
    };
    //alert(JSON.stringify(list));
    $.ajax({
        url: 'http://hfd.fangbanghui.com/index.php?m=Home&c=Index&a=rentajax',
        dataType: 'json',
        type: 'POST',
        data: list,
        success: function (data) {
            //alert(data);
            data = JSON.parse(data);
            //alert(data);
            //alert(data.datas);
            ajaxInfo(data.datas);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
//          alert(XMLHttpRequest.status);
//          alert(XMLHttpRequest.readyState);
//          alert(textStatus);
//          alert(XMLHttpRequest.responseText);
			alert("返回错误");
        }
    });

    //page select
    $('.page').click(function () {
        var page = $(this).html();
        var currentPage = 0;
        $('.page').each(function () {
            if($(this).hasClass('currentNum')) {
                currentPage = parseInt($(this).html());
            }
        });
        page = page === '上一页' ? currentPage - 1 : (page === '下一页' ? currentPage + 1 : page);
        list.page = page;
        $.ajax({
            url: 'http://hfd.fangbanghui.com/index.php?m=Home&c=Index&a=rentajax',
            dataType: 'json',
            type: 'POST',
            data: list,
            success: function (data) {
                //alert(data.length);
                $('.page').removeClass('currentNum');
                $(this).addClass('currentNum');
                data = JSON.parse(data);
                ajaxInfo(data.datas);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                 alert("提交错误");
            }
        });
    });

    /* type select */
    $('.typeSelect dl dd').click(function () {
        var n = $(this).index();
        $('.typeSelect dl dd').removeClass('current_dd');
        $(this).addClass('current_dd');
        list.rentType = n - 1;
        console.log(list);
        $.ajax({
            url: 'http://hfd.fangbanghui.com/index.php?m=Home&c=Index&a=rentajax',
            dataType: 'json',
            type: 'POST',
            data: list,
            success: function (data) {
                //alert(data.length);
                data = JSON.parse(data);
                ajaxInfo(data.datas);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                 alert("提交错误");
            }
        });
    });
    
    
    $('.area dd').click(function () {
        updateData($(this), $('.area dd'), 'area');
        console.log(list);
    });
    
    $('.rentPrice dd').click(function () {
        updateData($(this), $('.rentPrice dd'), 'price');
        console.log(list);
    });
    
    $('.room dd').click(function () {
        updateData($(this), $('.room dd'), 'room');
        console.log(list);
    });
    
    $('.square dd').click(function () {
        updateData($(this), $('.square dd'), 'square');
        console.log(list);
    });
    
    $('.direction dd').click(function () {
        updateData($(this), $('.direction dd'), 'direction');
        console.log(list);
    });
    
    
    
    
    /* sort area */
    $('.sortTitle div').click(function () {
        var n = $(this).index();
        $('.sortTitle div').removeClass('currentDiv');
        $(this).addClass('currentDiv');
        list.sortType = n;
        console.log(list);
        $.ajax({
            url: 'http://hfd.fangbanghui.com/index.php?m=Home&c=Index&a=rentajax',
            dataType: 'json',
            type: 'POST',
            data: list,
            success: function (data) {
                //alert(data.length);
                data = JSON.parse(data);
                ajaxInfo(data.datas);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                 alert(1);
            }
        });
    });
    
    
    
    
    
    
    
    
    function updateData (self, siblings, item) {
        var length = 0;
        var $_this = self;
        var $_all = siblings;
        var n = self.index();
        var content = self.html().replace('<i></i>','');
        for (var i = 1; i < $_all.length; i+=1){
            if($_all.eq(i).hasClass('checked')) {
                length += 1;
            }
        }

        if(n !== 1) {
            $_all.eq(0).removeClass('checked');
            if($_this.hasClass('checked') && length > 1) {
                $_this.removeClass('checked');
                length = length - 1;
                list[item].forEach(function (elem, index) {
                    if(elem === content) {
                        list[item].splice(index, 1);
                    }
                })
            }
            else if ($_this.hasClass('checked') && length === 1) {
                $_this.removeClass('checked');
                $_all.eq(0).addClass('checked');
                length = length - 1;
                list[item].forEach(function (elem, index) {
                    if(elem === content) {
                        list[item].splice(index, 1);
                    }
                })
            }
            else {
                $_this.addClass('checked');
                length += 1;
                list[item].push(content);
            }
        }
        else {
            $_all.removeClass('checked');
            $_all.eq(0).addClass('checked');
            list[item] = [];
        }

        var list2 = JSON.stringify(list);
        //alert(list2);
        $.ajax({
            url: 'http://hfd.fangbanghui.com/index.php?m=Home&c=Index&a=rentajax',
            dataType: 'json',
            type: 'POST',
            data: list,
            success: function (data) {
                //alert(data.length);
                data = JSON.parse(data);
                ajaxInfo(data.datas);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                 alert(1);
            }
        });
    }
    
    
    
    
    function ajaxInfo(data) {
        $('.sortList').children().remove();
        var listLength = data.length;
        for (var i = 0; i < listLength; i += 1) {
            var listPart = '<div class="listPart">';
            listPart += '<div class="listImg">';
            listPart += '<img src="http://hfd.fangbanghui.com/Public/' + data[i].imgurl.replace(data[i].imgurl[data[i].imgurl.length],'') + '" alt="">';
            //alert(data[i].imgurl);
            listPart += '</div>';
            listPart += '<div class="listInfo">';
            if(data[i]['title'] === '') {
                data[i]['title'] = '无介绍';
           // listPart += '<h4>' + data[i]['title'] + '</h4>'; 前台返回 data['content']的字段 @17-11-21
            //alert(data[i]['title']);
            listPart += '<div class="houseInfo"><span class="icon house"></span>';
            if(data[i].mianji === '') {
                data[i].mianji = '无介绍';
            }
            listPart += '<span class="square">' + data[i].mianji + '㎡</span>&nbsp;|&nbsp;';
            //alert(data[i].mianji);
            if(data[i].huxing === '') {
                data[i].huxing = '无介绍';
            }
            listPart += '<span class="houseType">' + data[i].huxing + '</span>&nbsp;|&nbsp;';
            //alert(data[i].huxing);
            if(data[i].floor === '') {
                data[i].floor = '无介绍';
            }
            listPart += '<span class="floor">' + data[i].floor + '</span>&nbsp;|&nbsp;';
            //alert(data[i].floor);
            if(data[i].zulinfs === '') {
                data[i].zulinfs = '无介绍';
            }
            listPart += '<span class="rentType">' + data[i].zulinfs + '</span></div>';
            //alert(data[i].zulinfs);
            listPart += '<div class="locationInfo"><span class="icon loca"></span>';
            if(data[i].dizhi === '') {
                data[i].dizhi = '无介绍';
            }
            listPart += '<span class="location">' + data[i].dizhi + '</span></div>';
            //alert(data[i].dizhi);
            listPart += '<div class="tabs">';
            data[i].peizhi = data[i].peizhi.split('|');
            for (var j = 0; j < data[i].peizhi.length; j += 1) {
                listPart += '<span class="tab">' + data[i].peizhi[j] + '</span>';
            }
            listPart += '</div></div>';
            listPart += '<div class="moreInfo"><div class="price">';
            listPart += '￥<span class="number">' + data[i].price + '</span>  <span>元/月</span>';
            //alert(data[i].price);
            listPart += '</div>';
            listPart += '<a href="http://hfd.fangbanghui.com/index.php?m=Home&c=Index&a=rentcontent&id=' + data[i]['id'] +'"><div class="moreButton">查看更多</div></a>';
            //alert(data[i]['id']);
            listPart += '</div>';
            listPart += '</div>';
            $('.sortList').append(listPart);
        }
        
        //alert(2);
    } 
});






